﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Exam.Models;

namespace Exam.Controllers
{
    public class HomeController : Controller
    {
        // GET: Home
        //学生登录方法
        public ActionResult LoginStudent()
        {
            return View();
        }
        [HttpPost]
        public ActionResult LoginStudent(string SturLoginName,string StuLoginPwd)
        {
            using (TestSystemEntities db = new TestSystemEntities())
            {
                Student student = db.Student.FirstOrDefault(t => t.SturLoginName == SturLoginName && t.StuLoginPwd == StuLoginPwd);
                if(student != null)
                {
                    Session["LoginID"] = student.StuID;
                    Session["LoginName"] = student.StuName;
                    Session["Type"] = "学生";
                    return RedirectToAction("Details/"+student.StuID,"Students");
                }
                ModelState.AddModelError("", "学生账号或密码错误！");
            }
            return View();
        }


        //老师登录方法
        public ActionResult LoginTeacher()
        {
            return View();
        }
        [HttpPost]
        public ActionResult LoginTeacher(string TeacherLoginName, string TeacherLoginPwd)
        {
            using (TestSystemEntities db = new TestSystemEntities())
            {
                Teacher teacher = db.Teacher.FirstOrDefault(t => t.TeacherLoginName == TeacherLoginName && t.TeacherLoginPwd == TeacherLoginPwd);
                if (teacher != null)
                {
                    Session["LoginID"] = teacher.TeacherID;
                    Session["LoginName"] = teacher.TeacherName;
                    Session["Type"] = "老师";
                    return RedirectToAction("Details/"+teacher.TeacherID,"Teachers");
                }
                ModelState.AddModelError("", "老师账号或密码错误！");
            }
            return View();
        }

        //退出系统
        public ActionResult Logout()
        {
            Session["LoginName"] = null;
            Session["Type"] = null;
            return RedirectToAction("Index","Teachers");
        }
    }
}